﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Follow.Business;
using Follow.Model;

namespace Follow.Adm
{
    public partial class Log : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["idUsuario"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                Common.ValidaPermissoes valida = new Common.ValidaPermissoes();
                bool possuiPermissao = valida.validaPermissaoPorTela("Log", Convert.ToInt32(Session["idGrupoUsuario"]));

                if (possuiPermissao == false)
                {
                    Response.Redirect("../permissaoNegada.aspx");
                }

                if (Request["Msg"] != null)
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = Request["Msg"].ToString();
                }
            }

            if (!IsPostBack)
            {
                DefinirDatas();
                PopulaLog(txtDataInicio.Text, txtDataFim.Text);
            }
        }

        protected void PopulaLog(String inicio, String fim)
        {
            TransacaoBO bo = new TransacaoBO();
            gvLog.DataSource = bo.findAll(Convert.ToDateTime(inicio), Convert.ToDateTime(fim));
            gvLog.DataBind();
        }

        protected void LimparView2()
        {
            hdnId.Value = "";
            txtDescricao.Text = "";
            txtUsuario.Text = "";
            txtData.Text = "";
        }

        protected void DefinirDatas()
        {
            DateTime hoje = DateTime.Now;
            txtDataInicio.Text = hoje.Subtract(new TimeSpan(7, 0, 0, 0)).ToShortDateString();
            txtDataFim.Text = hoje.Add(new TimeSpan(7, 0, 0, 0)).ToShortDateString();
        }

        protected void gvLog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("visualizar"))
            {
                Int32 id = Convert.ToInt32(this.gvLog.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

                if (id > 0 || id != null)
                {
                    Transacao t = new Transacao();
                    TransacaoBO bo2 = new TransacaoBO();
                    t = bo2.findById(id);
                    LimparView2();
                    hdnId.Value = t.IDTransacao.ToString();
                    txtData.Text = t.Data.ToString();
                    txtUsuario.Text = t.Usuario;
                    txtDescricao.Text = t.Comando;
                    mvLog.ActiveViewIndex = 1;
                }
            }
            
        }

        protected void btnPesquisa_Click(object sender, EventArgs e)
        {
            PopulaLog(txtDataInicio.Text, txtDataFim.Text);
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            LimparView2();
            mvLog.ActiveViewIndex = 0;
        }

        protected void gvLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvLog.PageIndex = e.NewPageIndex;
            PopulaLog(txtDataInicio.Text, txtDataFim.Text);
        }
    }
}